const http = require("http");
const querystring = require('querystring');
const parseUrl = require('url');
const hostname = "127.0.0.1";
const port = 8080;

http.createServer((request, response) => {
    console.log(querystring.parse(parseUrl.parse(request.url).query));
    var data = querystring.parse(parseUrl.parse(request.url).query);
    console.log(1111);
    request.on('data', function (chuck) {
        console.log(chuck);
    })
    // var Cookies = {};
    // request.headers.cookie && request.headers.cookie.split(';').forEach(function (Cookie) {
    //     var parts = Cookie.split('=');
    //     Cookies[parts[0].trim()] = (parts[1] || '').trim();
    // });
    // console.log(Cookies)
    response.writeHead(200, {
        'Content-Type': 'text/plain;charset=utf-8',
        'Access-Control-Allow-origin': 'http://127.0.0.1:5500',
        'Access-Control-Allow-Credentials': 'true',
        // 'Set-Cookie': 'user=luo; Path=/;Domain=http://127.0.0.1:5500;HttpOnly123456'  // HttpOnly的作用是让js无法读取cookie
    });


    response.end(data.callback + '({ "success": true, "user": "luo" })');
    // response.end('{ "success": true, "user": "luo" }');

}).listen(port, hostname, function () {
    console.log('Server running at http://127.0.0.1:8080');
});
